/**
 * Copyright (c) 2018 开源 All rights reserved.
 * <p>
 * https://www.chengcang.com
 * <p>
 * 版权所有，侵权必究！
 */

package com.chengcang.api.modules.sys.service.impl;

import com.chengcang.api.modules.sys.dao.SysTenantDao;
import com.chengcang.api.modules.sys.dao.SysUserDao;
import com.chengcang.api.modules.sys.dto.SysUserDTO;
import com.chengcang.api.modules.sys.entity.SysUserEntity;
import com.chengcang.api.modules.sys.service.SysDeptService;
import com.chengcang.api.modules.sys.service.SysDictDataService;
import com.chengcang.api.modules.sys.service.SysRoleUserService;
import com.chengcang.api.modules.sys.service.SysUserService;
import com.chengcang.common.enums.SuperAdminEnum;
import com.chengcang.common.service.impl.BaseServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import com.chengcang.common.utils.PasswordUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;


/**
 * 系统用户
 *
 * @author zhanli
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    private final SysRoleUserService sysRoleUserService;
    private final SysDeptService sysDeptService;
    private final SysDictDataService sysDictDataService;
    private final SysTenantDao sysTenantDao;



    @Override
    public SysUserDTO get(Long id) {
        SysUserEntity entity = baseDao.getById(id);

        SysUserDTO userDTO = ConvertUtils.sourceToTarget(entity, SysUserDTO.class);

        if(userDTO != null && userDTO.getDefaultTenantId() != null){
            userDTO.setTenantType(sysTenantDao.selectById(userDTO.getDefaultTenantId()).getTenantType());
        }
        return userDTO;
    }

    @Override
    public SysUserDTO getByUsername(String username) {
        SysUserEntity entity = baseDao.getByUsername(username);
        return ConvertUtils.sourceToTarget(entity, SysUserDTO.class);
    }

    @Override
    public SysUserDTO getByMobile(String mobile) {
        SysUserEntity entity = baseDao.getByMobile(mobile);
        SysUserDTO userDTO = ConvertUtils.sourceToTarget(entity, SysUserDTO.class);
        return userDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysUserDTO dto) {
        SysUserEntity entity = ConvertUtils.sourceToTarget(dto, SysUserEntity.class);

        //密码加密
        String password = PasswordUtils.encode(entity.getPassword());
        entity.setPassword(password);

        //保存用户
        entity.setDefaultUserType(SuperAdminEnum.NO.value());
        insert(entity);

        //保存角色用户关系
        sysRoleUserService.saveOrUpdate(entity.getId(), dto.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUserDTO dto) {
        SysUserEntity entity = ConvertUtils.sourceToTarget(dto, SysUserEntity.class);

        //密码加密
        if (StringUtils.isBlank(dto.getPassword())) {
            entity.setPassword(null);
        } else {
            String password = PasswordUtils.encode(entity.getPassword());
            entity.setPassword(password);
        }
        //更新用户
        updateById(entity);

        //更新角色用户关系
        sysRoleUserService.saveOrUpdate(entity.getId(), dto.getRoleIdList());
    }

    @Override
    public void delete(Long[] ids) {
        //删除用户
        baseDao.deleteBatchIds(Arrays.asList(ids));

        //删除角色用户关系
        sysRoleUserService.deleteByUserIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePassword(Long id, String newPassword) {
        newPassword = PasswordUtils.encode(newPassword);

        baseDao.updatePassword(id, newPassword);
    }

    @Override
    public int getCountByDeptId(Long deptId) {
        return baseDao.getCountByDeptId(deptId);
    }

    @Override
    public List<Long> getUserIdListByDeptId(List<Long> deptIdList) {
        return baseDao.getUserIdListByDeptId(deptIdList);
    }

    @Override
    public void forbidTenant(Long[] ids, Integer status, String remark) {
        for (Long id : ids) {
            SysUserEntity userEntity = baseDao.getById(id);
            if (userEntity.getDefaultTenantId() != null) {
                SysUserEntity sysUserEntity = new SysUserEntity();
                sysUserEntity.setStatus(status);
                sysUserEntity.setId(id);
                sysUserEntity.setRemark(remark);
                baseDao.updateById(sysUserEntity);
            }
        }
    }

}
